<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'recharge',
      dialogValue: false,
      ruleForm: {
        user_id: '',
        amount: 0,
        type: 1,
        desc: ''
      },
      defaultForm: {
        user_id: '',
        amount: 0,
        type: 1,
        desc: ''
      },
      rules: {
        user_id: [{ required: true, message: '不能为空' }],
        amount: [{ required: true, message: '不能为空' }],
        type: [{ required: true, message: '不能为空' }]
      },
      formList: [
        {
          prop: 'user_id',
          name: 'select',
          label: '企业用户',
          colSpan: 24,
          options: [],
        },
        {
          prop: 'type',
          name: 'select',
          label: '类型',
          colSpan: 24,
          options: [
            {
              value: 1,
              label: '充值'
            },
            {
              value: 2,
              label: '手动扣款'
            }
          ],
        },
        {
          prop: 'amount',
          name: 'input',
          label: '金额',
          colSpan: 24,
          type: 'number',
        },
        {
          prop: 'desc',
          name: 'input',
          label: '备注',
          colSpan: 24,
          type: 'textarea',
        }
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      console.log('row',row)
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      http.user.list().then(res => {
        this.formList[0].options = res.data.map(item => {
          return {
            label: item.company_name,
            value: item.id
          }
        })
      })
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
